Class BoxAid
static var birthNode;
static var inited=false;
static var headStyle;
static function init(){
	if(Me.inited) return;
	Me.inited=true;
	Me.headStyle=document.createElement("style");
	document.head.appendChild(Me.headStyle);
	Me.headStyle.sheet.insertRule(
		"div.__box{position:absolute;vertical-align:middle;display:block;"
		+"font-family:"+(G.os==1?"Microsoft Yahei,PingFangSC-Light":"")+";"
		+"background-repeat:no-repeat;"
		+G.toCssKey("user-select")+":none;"
		+G.toCssKey("transform-origin")+":0 0;"+G.toCssKey("box-sizing")+":border-box;"
		+G.toCssKey("tap-highlight-color")+":rgba(0,0,0,0);"+G.toCssKey("overflow-scrolling")+":touch;"
		+G.toCssKey("touch-callout")+":none}",
		0
	);
	if(G.os==3){
		Me.headStyle.sheet.insertRule("div::"+G.toCssKey("scrollbar")+"{width:0px}",0);
		Me.headStyle.sheet.insertRule("input::"+G.toCssKey("input-placeholder")+"{color:#a0a0a0}",0);
	}

	document.body.style.cssText="margin:0;overflow:hidden;"+G.toCssKey("text-size-adjust")+":100% !important";

	Me.birthNode=document.createElement("div");
	document.body.appendChild(Me.birthNode);
	Me.birthNode.style.visibility="hidden";

	G.canvas=document.createElement("canvas");
	G.bgStyle["star"]=[[50,0],[38,38],[0,38],[31,60],[19,96],[50,75],[81,96],[70,60],[100,38],[62,38],[50,0]];
}
static function addAML(caller,items){
	var queues=[[items,null]];
	var resizeBoxs=[];
	var firstBox;
	while(queues.length>0){
		var itemArr=queues.pop();
		var arr=itemArr[0];
		if(arr[0]==null) continue;
		var parentArr=itemArr[1];
		if(!parentArr){
			arr.unshift(caller);
		}
		var args=null;
		if(typeof(arr[0])=="object"&&!is(Array,arr[0])&&!arr[0].__isBox){
			args=arr[0];
			arr[0]=new Box();
		}
		if(typeof(arr[1])=="object"&&!is(Array,arr[1])&&arr[1].doif==null){
			args=arr[1];
		}
		if(is(String,arr[0])){
			arr[0]=new Img(arr[0]);
		}
		else if(is(Array,arr[0])){
			arr[0]=new Label(arr[0][0],arr[0][1],arr[0][2]);
		}
		else if(arr[0].__isClass){
			if(args&&args.idx) Class.test=true;
			arr[0]=Class.new(arr[0]);
			Class.test=false;
		}
		var box=arr[0];
		if(box!=caller&&!firstBox) firstBox=box;
		for(var i=1;i<arr.length;i++){
			var subArr=arr[i];
			if(subArr.hasOwnProperty("doif")&&!subArr.doif){
				i++;
			}
			if(!is(Array,subArr)) continue;
			queues.unshift([subArr,itemArr]);
		}
		var hasSpecial=false;
		if(args){
			if(args.style){
				var style=G.style[args.style];
				for(var k in style){
					if(args[k]==null) args[k]=style[k];
				}
			}
			for(var k in args){
				var v=args[k];
				if(k=="dim"){
					var dimTo=caller.dimTo||caller;
					dimTo[v]=box;
				}
				else if(k=="x"||k=="y"||k=="w"||k=="h"||k=="pos"||k=="size"||k=="bg"){
					hasSpecial=true;
				}
				else{
					var func=box["set"+k.charAt(0).toUpperCase()+k.slice(1)];
					if(typeof(func)=="function"){
						func.call(box,args[k]);
					}
					else{
						box[k]=args[k];
					}
				}
			}
		}
		if(parentArr){
			var parentBox=parentArr[0][0];
			parentBox.addChild(box);
		}
		if(parentBox&&parentBox.relative==1&&box.__class==Box&&box.w==0&&args&&!args.w){
			box.setW("f");
		}
		if(hasSpecial){
			if(args.w!=null||args.h!=null){
				box.setSize(args.w==null?box._w:args.w,args.h==null?box._h:args.h);
			}
			if(args.size!=null) box.setSize.apply(box,args.size);
			if(args.x!=null) box.setX(args.x);
			if(args.y!=null) box.setY(args.y);
			if(args.pos!=null) box.setPos.apply(box,args.pos);
			if(args.bg!=null) box.setBg(args.bg);
		}
		if(box._w==0) resizeBoxs.unshift(box);
	}
	for(var i=0;i<resizeBoxs.length;i++){
		var box=resizeBoxs[i];
		var child=box.children[0];
		if(!child||child.w==0) continue;
		box.setSize(child.w,child.h);
	}
	return firstBox;
}
static function drawBgShape(caller,arr){
	G.canvas.width=caller.w;
	G.canvas.height=caller.h;
	var sp=[];
	var ctx=G.canvas.getContext("2d");
	ctx.clearRect(0,0,caller.w,caller.h);
	ctx.beginPath();
	for(var i=2;i<arr.length;i++){
		var a=arr[i];
		if(i==2||a==0){
			a=arr[i-1];
			if(is(Array,a)&&sp[0]==a[0]&&sp[1]==a[1]){
				ctx.closePath();
			}
			i++;
			a=arr[i];
			if(!a) continue;
			sp[0]=a[0];
			sp[1]=a[1];
			if(is(Array,a)) ctx.moveTo(a[0],a[1]);
			continue;
		}
		if(a==1) continue;
		if(is(Array,a)){
			ctx.lineTo(a[0],a[1]);
			continue;
		}
		var t=a;
		var x1=arr[i-1][0];
		var y1=arr[i-1][1];
		if(!is(Array,arr[i+1])) continue;
		var x2=arr[i+1][0];
		var y2=arr[i+1][1];
		if(t==2||t==3){
			var r=(Math.abs(x2-x1)+Math.abs(y2-y1))/2;
			if(y1==y2){
				ctx.arcTo(x1,t==2?y1-r:y1+r,(x2+x1)/2,t==2?y2-r:y2+r,r);
				ctx.arcTo(x2,t==2?y1-r:y1+r,x2,y2,r);
			}
			else if(x1==x2){
				ctx.arcTo(t==2?x1-r:x1+r,y1,t==2?x1-r:x1+r,(y1+y2)/2,r);
				ctx.arcTo(t==2?x1-r:x1+r,y2,x2,y2,r);
			}
			else{
				if(Math.abs(x2-x1)>Math.abs(y2-y1)){
					r=Math.abs(y2-y1);
					var x3=x2+(x2>x1?-1:1)*r;
					ctx.lineTo(x3,y1);
					x1=x3;
				}
				else if(Math.abs(x2-x1)<Math.abs(y2-y1)){
					r=Math.abs(x2-x1);
					var y3=y2+(y2>y1?-1:1)*r;
					ctx.lineTo(x1,y3);
					y1=y3;
				}
				ctx.arcTo(t==2?x2:x1,t==2?y1:y2,x2,y2,r);
			}
			i++;
		}
		else if(t==4){
			if(!is(Array,arr[i+2])) continue;
			var x3=arr[i+2][0];
			var y3=arr[i+2][1];
	        var e=((x1*x1-x2*x2)+(y1*y1-y2*y2))/2;
	        var f=((x1*x1-x3*x3)+(y1*y1-y3*y3))/2;
	        var x0=-((y1-y3)*e-(y1-y2)*f)/((y1-y2)*(x1-x3)-(x1-x2)*(y1-y3));
        	var y0=-((x1-x2)*f-(x1-x3)*e)/((y1-y2)*(x1-x3)-(x1-x2)*(y1-y3));
        	var r=Math.distance(x0,y0,x1,y1);
        	var ag1=Math.atan2(y1-y0,x1-x0)*180/Math.PI;
        	var ag2=Math.atan2(y2-y0,x2-x0)*180/Math.PI;
        	var ag3=Math.atan2(y3-y0,x3-x0)*180/Math.PI;
        	if(ag1<0) ag1+=360;
        	if(ag2<0) ag2+=360;
        	if(ag3<0) ag3+=360;
        	var dir=1;
        	if(ag2<ag1&&(ag3>ag1||ag3<ag2)) dir=-1;
        	if(ag2>ag3&&ag3>ag1) dir=-1;
			var ag0=ag3-ag1;
			if(ag0<0) ag0+=360;
			if(dir==-1) ag0=360-ag0;
			for(var j=0;j<3;j++){
				ag2=ag0;
				if(ag2<=120){
					ag4=ag1+dir*ag2/2;
					var r4=r/Math.cos(ag2*Math.PI/360);
					var x4=x0+r4*Math.cos(ag4*Math.PI/180);
					var y4=y0+r4*Math.sin(ag4*Math.PI/180);
					ctx.arcTo(x4,y4,x3,y3,r);
					break;
				}
				ag2=120;
				ag0-=120;
				ag4=ag1+dir*ag2/2;
				ag1+=dir*ag2;
				var r4=r/Math.cos(ag2*Math.PI/360);
				var x4=x0+r4*Math.cos(ag4*Math.PI/180);
				var y4=y0+r4*Math.sin(ag4*Math.PI/180);
				var x5=x0+r*Math.cos(ag1*Math.PI/180);
				var y5=y0+r*Math.sin(ag1*Math.PI/180);
				ctx.arcTo(x4,y4,x5,y5,r);
			}
			i+=2;
		}
		else if(t==5){
			if(!is(Array,arr[i+2])) continue;
			var x3=arr[i+2][0];
			var y3=arr[i+2][1];
			var x4=(4*x2-x1-x3)/2;
			var y4=(4*y2-y1-y3)/2;
			ctx.quadraticCurveTo(x4,y4,x3,y3);
			i+=2;
		}
	}
	var a=arr[arr.length-1];
	if(a[0]==sp[0]&&a[1]==sp[1]){
		ctx.closePath();
	}
	if(arr[0]){
		var c=arr[0];
		if(!is(Array,c)){
			ctx.fillStyle=G.toColor(c);
		}
		else{
			var t=c[c.length-1];
			if(!is(Number,t)){
				c.push(1);
				t=1;
			}
			if(t==2){
				var g=ctx.createLinearGradient(caller.w,0,0,caller.h);
			}
			else if(t==3){
				g=ctx.createLinearGradient(caller.w,0,0,0);
			}
			else if(t==4){
				g=ctx.createLinearGradient(caller.w,caller.h,0,0);
			}
			else{
				g=ctx.createLinearGradient(0,0,caller.w,0);
			}
			for(var i=0;i<c.length-1;i++){
				var s=c[i];
				var n=s.charAt(0);
				if(i==0) n=0;
				if(i==c.length-2) n=10;
				if(n=="#") continue;
				g.addColorStop(n/10,G.toColor(s.replace(/^\d+/g,"")));
			}
			ctx.fillStyle=g;
		}
		ctx.fill();
	}
	if(arr[1]){
		var s=arr[1];
		if(is(Array,s)){
			s=s[0];
			ctx.lineJoin="round";
			ctx.lineCap="round";
		}
		var n=s.indexOf("#");
		ctx.strokeStyle=G.toColor(s.slice(n));
		ctx.lineWidth=int(s.slice(0,n))||1;
		ctx.stroke();
	}
	caller.div.style.backgroundImage="url("+G.canvas.toDataURL("image/png")+")";
}